package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Resource
    private AddressBookMapper addressBookMapper;

    @Override
    public R modify(AddressBook addressBook) {
        if (null == addressBook){
            return R.error("参数错误");
        }
        AddressBook oldAddress = addressBookMapper.selectById(addressBook.getId());
        if (oldAddress != addressBook){
            addressBookMapper.updateById(addressBook);
        }

        return R.success("地址更新成功");
    }

    @Override
    public R deleteById(Long[] ids) {
        if (null == ids){
            return R.error("参数错误");
        }
        Arrays.stream(ids).forEach(id->{
            addressBookMapper.deleteById(id);
        });

        return R.success("删除成功");
    }
}